/// <reference path="jquery.d.ts"/>
/// <reference path="jqueryui.d.ts"/>
var __extends = this.__extends || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
};
var SpreedSheet;
(function (SpreedSheet) {
    var StringStorage = (function () {
        function StringStorage() {
            this.count = 0;
            this.lines = [];
            this.isReadyFlag = false;
        }
        StringStorage.prototype.isReady = function () {
            return this.isReadyFlag;
        };
        StringStorage.prototype.start = function () {
            this.count = 0;

            //            this.lines = this.allText.split("|");
            this.isReadyFlag = true;
        };
        StringStorage.prototype.stop = function () {
        };
        StringStorage.prototype.save = function (value) {
            this.lines.push(value);
            this.count++;
        };
        StringStorage.prototype.load = function () {
            this.count++;
            return this.lines[this.count - 1];
        };
        return StringStorage;
    })();
    SpreedSheet.StringStorage = StringStorage;
    var DefaultStorage = (function (_super) {
        __extends(DefaultStorage, _super);
        function DefaultStorage() {
            _super.apply(this, arguments);
            this.defaultText = "10|10|7|6||||Sample Bill|||||||||No.|Product|Units|Price|Net||||||1|Paper|12|1|={<}*{<<}||||||={^}+1|Pencil|3|4|={<}*{<<}||||||={^}+1|Pen|3|5|={<}*{<<}||||||={^}+1|Books|2|80|={<}*{<<}|||||||||Sum|={^}+{^^}+{^^^}+{^^^^}||||||||||||||||||||||||||||||||||||";
        }
        DefaultStorage.prototype.start = function () {
            this.count = 0;
            this.lines = this.defaultText.split("|");
            this.isReadyFlag = true;
        };
        return DefaultStorage;
    })(StringStorage);
    SpreedSheet.DefaultStorage = DefaultStorage;

    var WebStorage = (function () {
        function WebStorage() {
            this.savetext = "";
            this.start();
        }
        WebStorage.prototype.isReady = function () {
            return true;
        };
        WebStorage.prototype.start = function () {
            this.count = 1;
        };
        WebStorage.prototype.stop = function () {
        };
        WebStorage.prototype.save = function (value) {
            window.localStorage.setItem(this.count.toString(), value);
            this.count++;
        };
        WebStorage.prototype.load = function () {
            this.count++;
            return window.localStorage.getItem((this.count - 1).toString());
        };
        return WebStorage;
    })();
    SpreedSheet.WebStorage = WebStorage;

    var Cell = (function () {
        function Cell(currentRow, currentColumn) {
            this.currentRow = currentRow;
            this.currentColumn = currentColumn;
            this.page = "";
            this.value = 0;
            this.formula = "";
        }
        Cell.prototype.setFormula = function (str) {
            str = str.replace('\n', '');
            str = str.replace('\r', '');
            this.formula = str;
        };
        Cell.prototype.getFormula = function () {
            return this.formula;
        };
        Cell.prototype.save = function (media) {
            media.save(this.formula);
        };

        Cell.prototype.load = function (media) {
            this.setFormula(media.load());
        };

        Cell.prototype.assumeData = function () {
            this.formula = "";
            this.fromFormulaToValueToPage();
        };

        Cell.prototype.fromFormulaToValueToPage = function () {
            if (this.formula.substring(0, 1) == '=') {
                this.value = 0;
                this.page = this.formula.substring(1);
                if (atable) {
                    atable.evaluateFormula(this.currentRow, this.currentColumn);
                }
            } else if (this.formula.substring(0, 1) == '\'') {
                this.value = 0;
                this.page = this.formula.substring(1);
            } else {
                try  {
                    this.value = parseFloat(this.formula);
                    if (isNaN(this.value)) {
                        this.value = 0;
                        this.page = this.formula;
                    } else {
                        this.page = this.value.toString();
                    }
                } catch (Exception) {
                    this.value = 0;
                    this.page = this.formula;
                }
            }
        };
        Cell.prototype.makeDom = function () {
            return ($('<td></td>').text(this.formula).addClass('column-' + this.currentColumn).addClass('row-' + this.currentRow).data('column', this.currentColumn).data('row', this.currentRow));
        };

        Cell.prototype.select = function () {
            var classRow = '.' + 'row-' + this.currentRow;
            var classColumn = '.' + 'column-' + this.currentColumn;
            return $(classRow + classColumn);
        };

        Cell.prototype.calcAll = function () {
            this.fromFormulaToValueToPage();
            this.select().text(this.page);
        };
        return Cell;
    })();
    SpreedSheet.Cell = Cell;

    var Row = (function () {
        function Row(countColumn, currentRow) {
            this.countColumn = countColumn;
            this.currentRow = currentRow;
            this.cells = [];
        }
        Row.prototype.save = function (media) {
            for (var i = 0; i < this.cells.length; i++) {
                this.cells[i].save(media);
            }
        };

        Row.prototype.load = function (media) {
            this.cells = [];
            for (var i = 0; i < this.countColumn; i++) {
                var newCell = new Cell(this.currentRow, i);
                newCell.load(media);
                this.cells.push(newCell);
            }
        };

        Row.prototype.assumeData = function () {
            for (var i = 0; i < this.countColumn; i++) {
                var newCell = new Cell(this.currentRow, i);
                newCell.assumeData();
                this.cells.push(newCell);
            }
        };
        Row.prototype.calcAll = function () {
            for (var i = 0; i < this.cells.length; i++) {
                this.cells[i].calcAll();
            }
        };

        Row.prototype.makeDom = function () {
            var jqRow = $('<tr></tr>');
            for (var i = 0; i < this.cells.length; i++) {
                jqRow.append(this.cells[i].makeDom());
            }
            return jqRow;
        };
        return Row;
    })();
    SpreedSheet.Row = Row;

    var Table = (function () {
        function Table() {
            this.currentRow = 0;
            this.currentColumn = 0;
            this.countRow = 10;
            this.countColumn = 10;
            this.rows = [];
            this.storage = new WebStorage();
            this.defaultStorage = new DefaultStorage();
            this.assumeData();
            var test = window.localStorage.getItem("1");
            if (test != "10") {
                this.loadDefault();
            } else {
                this.load();
            }
        }
        Table.prototype.save = function (media) {
            if (typeof media === "undefined") { media = this.storage; }
            media.start();
            while (!media.isReady()) {
            }
            media.save(this.countRow.toString());
            media.save(this.countColumn.toString());
            media.save(this.currentRow.toString());
            media.save(this.currentColumn.toString());
            for (var i = 0; i < this.rows.length; i++) {
                this.rows[i].save(media);
            }
            media.stop();
        };
        Table.prototype.loadDefault = function () {
            this.load(this.defaultStorage);
        };
        Table.prototype.load = function (media) {
            if (typeof media === "undefined") { media = this.storage; }
            media.start();
            while (!media.isReady()) {
            }
            this.countRow = parseInt(media.load());
            this.countColumn = parseInt(media.load());
            this.currentRow = parseInt(media.load());
            this.currentColumn = parseInt(media.load());
            this.rows = [];
            for (var i = 0; i < this.countRow; i++) {
                var newRow = new Row(this.countColumn, i);
                newRow.load(media);
                this.rows.push(newRow);
            }
            media.stop();
            this.refreshPage();
        };

        Table.prototype.assumeData = function () {
            for (var i = 0; i < this.countRow; i++) {
                var newRow = new Row(this.countColumn, i);
                newRow.assumeData();
                this.rows.push(newRow);
            }
        };
        Table.prototype.plugAllEvent = function () {
            this.plugOkCancel();
            this.plugKeyPress();
            this.plugSaveLoad();
        };
        Table.prototype.refreshPage = function () {
            this.clearTable();
            this.makeDom();
            this.focusCurrent();
            this.refreshClickable();

            this.calcAll();
            $('#ibar').focus();
        };
        Table.prototype.evaluateRelation = function (irow, icolumn, relation) {
            var y = 0;
            var x = 0;
            for (var i = 0; i < relation.length; i++) {
                switch (relation.charAt(i)) {
                    case '<':
                        x--;
                        break;
                    case '>':
                        x++;
                        break;
                    case '^':
                        y--;
                        break;
                    case 'v':
                        y++;
                        break;
                }
            }
            var isRelative = (x != 0 || y != 0);
            if (isRelative && irow + y >= 0 && icolumn + x >= 0) {
                return this.rows[irow + y].cells[icolumn + x].value.toString();
            }
            var word = relation.substring(1, relation.length - 1).toUpperCase();
            if (word == "SUMUP") {
                return this.sumUp(icolumn, irow).toString();
            } else if (word == "SUMLEFT") {
                return this.sumRight(icolumn, irow).toString();
            }

            return "0";
        };
        Table.prototype.sumUp = function (column, row) {
            var sum = 0.0;
            for (var irow = 0; irow < row; irow++) {
                sum = sum + (this.rows[irow].cells[column].value);
            }
            return sum;
        };
        Table.prototype.sumRight = function (column, row) {
            var sum = 0.0;
            for (var icol = 0; icol < column; icol++) {
                sum += (this.rows[row].cells[icol].value);
            }
            return sum;
        };
        Table.prototype.calcAll = function () {
            for (var irow = 0; irow < this.rows.length; irow++) {
                this.rows[irow].calcAll();
            }
        };
        Table.prototype.evaluateFormula = function (irow, icolumn) {
            var formula = this.rows[irow].cells[icolumn].getFormula();
            if (formula.substring(0, 1) == '=') {
                formula = formula.substring(1);
                var regEx = new RegExp('{.*?}');
                try  {
                    do {
                        var regExArray = regEx.exec(formula);
                        if (regExArray.length <= 0) {
                            break;
                        }
                        var relationValue = this.evaluateRelation(irow, icolumn, regExArray[0]);
                        formula = formula.replace(regExArray[0], relationValue);
                    } while(true);
                } catch (Exception) {
                }
            }
            var value = eval(formula);
            if (!isNaN(value)) {
                this.rows[irow].cells[icolumn].value = value;
                this.rows[irow].cells[icolumn].page = value.toString();
                var page = value.toString();
                this.rows[irow].cells[icolumn].select().text(page);
            }
        };
        Table.prototype.plugKeyPress = function () {
            var that = this;
            $('body').keydown(function (e) {
                switch (e.keyCode) {
                    case 8:
                    case 46:
                        $('#ibar').text('');
                        break;
                }
                switch (e.keyCode) {
                    case 8:
                    case 46:
                    case 13:
                    case 37:
                    case 38:
                    case 39:
                    case 40:
                        that.rows[that.currentRow].cells[that.currentColumn].setFormula($('#ibar').text());
                        that.rows[that.currentRow].cells[that.currentColumn].calcAll();
                        that.calcAll();
                        break;
                }
                switch (e.keyCode) {
                    case 37:
                        that.unfocusCurrent();
                        that.currentColumn--;
                        if (that.currentColumn < 0) {
                            that.currentColumn = 0;
                        }
                        that.focusCurrent();

                        break;
                    case 38:
                        that.unfocusCurrent();
                        that.currentRow--;
                        if (that.currentRow < 0) {
                            that.currentRow = 0;
                        }
                        that.focusCurrent();

                        break;
                    case 39:
                        that.unfocusCurrent();
                        that.currentColumn++;
                        if (that.currentColumn >= that.countColumn) {
                            that.currentColumn = that.countColumn - 1;
                        }
                        that.focusCurrent();

                        break;
                    case 13:
                    case 40:
                        that.unfocusCurrent();
                        that.currentRow++;
                        if (that.currentRow >= that.countRow) {
                            that.currentRow = that.countRow - 1;
                        }
                        that.focusCurrent();

                        break;
                }

                e.isDefaultPrevented = function () {
                    switch (e.keyCode) {
                        case 8:
                        case 46:
                        case 13:
                        case 37:
                        case 38:
                        case 39:
                        case 40:
                            return true;
                            break;
                        default:
                            break;
                    }
                    return false;
                };
            });
        };
        Table.prototype.plugSaveLoad = function () {
            var that = this;
            $('#isave').click(function () {
                that.save();
            });
            $('#iload').click(function () {
                that.load();
            });
        };
        Table.prototype.plugOkCancel = function () {
            var that = this;
            $('#iok').click(function () {
                that.rows[that.currentRow].cells[that.currentColumn].setFormula($('#ibar').text());
                that.rows[that.currentRow].cells[that.currentColumn].calcAll();
                that.calcAll();
            });
            $('#icancel').click(function () {
                $('#ibar').text(that.rows[that.currentRow].cells[that.currentColumn].getFormula());
            });
        };
        Table.prototype.unfocusCurrent = function () {
            $('tr').eq(this.currentRow).children('td').eq(this.currentColumn).css('background-color', 'white');
        };
        Table.prototype.focusCurrent = function () {
            $('tr').eq(this.currentRow).children('td').eq(this.currentColumn).css('background-color', 'yellow');
            this.showOnBar();
        };

        Table.prototype.showOnBar = function () {
            $('#ibar').text(this.rows[this.currentRow].cells[this.currentColumn].getFormula());
        };

        Table.prototype.refreshClickable = function () {
            var that = this;
            $('td').unbind('click');
            $('td').click(function (e) {
                that.unfocusCurrent();
                that.currentRow = $(this).data('row');
                that.currentColumn = $(this).data('column');
                that.focusCurrent();
            });
        };

        Table.prototype.clearTable = function () {
            $('#itable').children().remove();
        };

        Table.prototype.makeDom = function () {
            var that = this;

            var jqRow = $('#itable');
            for (var i = 0; i < this.rows.length; i++) {
                jqRow.append(that.rows[i].makeDom());
            }
            return jqRow;
        };
        return Table;
    })();
    SpreedSheet.Table = Table;
    var passed = false;
    var atable = new Table();

    $(document).ready(function () {
        atable.plugAllEvent();
        atable.refreshPage();
        $('#islot').click(function () {
            $('#dialogSlot').dialog();
        });
    });
})(SpreedSheet || (SpreedSheet = {}));
//# sourceMappingURL=app.js.map
